//
//  MkView.swift
//  onemeetingmac
//
//  Created by 张衡 on 2019/12/19.
//  Copyright © 2019 onemeeting. All rights reserved.
//

import Foundation

@objc protocol MVViewDelegate {
    func mvmouseDown(with event: NSEvent,offsetPoint:NSPoint)
}

class MVView: MSView {
    
    var isEnableMove = true
    var firstMouseDownPoint:NSPoint = NSPoint.zero
    
    weak var delegate:MVViewDelegate?
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        //self.isImpl = true
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func mouseDown(with event: NSEvent) {
        super.mouseDown(with: event)
        firstMouseDownPoint = (self.window?.contentView?.convert(event.locationInWindow, to: self))!
        print(#function)
    }

    
    
    override func mouseDragged(with event: NSEvent) {
        super.mouseDragged(with: event)
        
        NSCursor.crosshair.set()
        let newPoint = self.window?.contentView?.convert(event.locationInWindow, to: self)
        let offset = NSPoint(x: newPoint!.x - firstMouseDownPoint.x, y: newPoint!.y - firstMouseDownPoint.y)

        let origin = self.frame.origin
        let size = self.frame.size
        self.frame = NSRect(x: origin.x + offset.x, y: origin.y + offset.y, width: size.width, height: size.height)
        if isEnableMove == true {
            self.delegate?.mvmouseDown(with: event, offsetPoint: offset)
        }
        print(#function)
        
    }

}
